package com.itheima.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealMapper;
import com.itheima.pojo.*;
import com.itheima.utils.JedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    JedisUtil jedisUtil;
    @Autowired
    SetmealService setmealService;

    @Override
    public List<CheckGroup> findAllCheckGroup() {

        return setmealMapper.findAllCheckGroup();
    }

    @Override
    public void addSetmeal(Setmeal setmeal) {
        setmealMapper.addSetmeal(setmeal);
        setmealMapper.addSetmealAndCheckGroup(setmeal.getId(), setmeal.getCheckgroupIds());

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        setmealMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public JSONArray getSetmeal() {
        String setmeal = jedisUtil.get("setmeal");
        if(setmeal==null||setmeal.length()==0){
            List<Setmeal> setmeals = setmealMapper.getSetmeal();
            jedisUtil.sadd("setmeal", JSON.toJSONString(setmeals));
            return JSONUtil.parseArray(setmeals);
        }

        JSONArray setmeals= JSONUtil.parseArray(setmeal);

        return  JSONUtil.parseArray(setmeal);
    }

    @Override
    public JSONObject findById(int id) {
        //从redisz中取出套餐
        String setmeals = jedisUtil.get(("setmeal" + id));
//判断在redis中是否有值
        if(null == setmeals||setmeals.length()==0){
            Setmeal setmeal = setmealMapper.findById(id);
            List<CheckGroup> checkGroups = setmealMapper.findCheckGroupById(id);
            if (CollectionUtil.isNotEmpty(checkGroups)) {
                for (CheckGroup checkGroup : checkGroups) {
                    List<CheckItem> checkItems = setmealMapper.findCheckItemById(checkGroup.getId());
                    checkGroup.setCheckItems(checkItems);
                }
                setmeal.setCheckGroups(checkGroups);
            }
            setmeals = JSON.toJSONString(setmeal);
            jedisUtil.sadd(("setmeal" + id), setmeals);

        }
        JSONObject arry = JSONUtil.parseObj(setmeals);
        return arry;
    }

    @Override
    public Setmeal findByIdBatch(int id) {
        Setmeal setmeal = setmealMapper.findById(id);
        List<CheckGroup> checkGroups = setmealMapper.findCheckGroupById(id);
        List<Integer> ids = new ArrayList<>();
        for (CheckGroup checkGroup : checkGroups) {
            ids.add(checkGroup.getId());
        }
        List<CheckItem> checkItems = setmealMapper.findCheckItemsByCheckGroupIds(ids);
        Map<Integer, List<CheckItem>> collect = checkItems.stream().collect(Collectors.groupingBy(CheckItem::getCheckGroupId));
        for (CheckGroup checkGroup : checkGroups) {
            checkGroup.setCheckItems(collect.get(checkGroup.getId()));
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }

    @Override
    public Setmeal findDetailById(int id) {
        return setmealMapper.findById(id);
    }


    /**
     * 更新套餐信息
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void update(Setmeal setmeal, Integer[] checkgroupIds) {
        //更新套餐信息
        setmealMapper.update(setmeal);

    }

    /**
     * 删除套餐
     * @param id
     */
    @Override
    public void deleteById(int id) {
        //先根据套餐id删除中间表t_package_checkgroup套餐和检查组的关系
        setmealMapper.deleteCheckGroupByPkgId(id);
        //再根据id删除套餐信息
        setmealMapper.deleteById(id);
    }

    @Override
    public List<Integer> getCheckGroupIdByPkgId(int id) {

        return setmealMapper.getCheckGroupIdByPkgId(id);
    }


}
